/*
 * main.cpp
 *
 *  Created on: Jun 21, 2011
 *      Author: blyth
 */

#include <DataReader.h>
#include <TApplication.h>
#include <TH1D.h>
#include <TFile.h>
#include <TCanvas.h>
#include <signal.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

void TCanvas::Closed() {
	Emit("Closed()");

	gApplication->Terminate(0);
}

int main(int argc, char** argv) {
	double biggest = 0;
	int bigL;
	int bigI;
	DataReader* dr = new DataReader(argv[1], atoi(argv[2]));
	double* sum;
	dr->GetData(0, sum);

	TFile* file = new TFile("distrib.root", "recreate");
	TH1D* histo = new TH1D("CEDistrib", "CEDistrib", 130000, 0, 1300);
	TH1D* histo1 = new TH1D("PEDistrib", "PEDistrib", 190000, 0, 1900);

	for (int l = 0; sum != NULL; l++) {
		cout << "\r" << l << flush;

		double total = 0;
		for (int i = 0; i < 2716; i++) {
			double energy = sum[i];
			total += energy;
			histo->Fill(energy);
			if (energy > biggest) {
				biggest = energy;
				bigL = l;
				bigI = i;
			}
		}
		histo1->Fill(total);

		delete[] sum;
		dr->GetData(l + 1, sum);
	}

	histo->Write();
	histo1->Write();
	file->Close();

	delete dr;
	cout << endl << biggest << " MeV\tEvent: " << bigL << "\tCrystal: " << bigI << endl;
	return 0;
}
